package edu.study.common.util.math;

import java.util.HashMap;
import java.util.Map;

public class ChineseConvert {
	
	private static final Map<String, String> NUMBER_MAPPING = new HashMap<String, String>();
	
	static {
		NUMBER_MAPPING.put("0", "零");
		NUMBER_MAPPING.put("1", "壹");
		NUMBER_MAPPING.put("2", "贰");
		NUMBER_MAPPING.put("3", "叁");
		NUMBER_MAPPING.put("4", "肆");
		NUMBER_MAPPING.put("5", "伍");
		NUMBER_MAPPING.put("6", "陆");
		NUMBER_MAPPING.put("7", "柒");
		NUMBER_MAPPING.put("8", "捌");
		NUMBER_MAPPING.put("9", "玖");
	}

	public static String[] getCp(String number) {
		char[] rslt = number.toCharArray();
		int[] array = new int[rslt.length];
		for (int i = 0; i < rslt.length; i++) {
			array[i] = rslt[i] - 48;
		}
		String[] result = new String[] { " ", " ", " ", " ", " ", " ", " ", " ", " " };
		int length = result.length - array.length;
		for (int i = 0; i < array.length; i++) {
			result[i + length] = NUMBER_MAPPING.get(String.valueOf(array[i]));
		}
		return result;
	}
	
	public static String[] getCp2(String number) {
		char[] rslt = number.toCharArray();
		int[] array = new int[rslt.length];
		for (int i = 0; i < rslt.length; i++) {
			array[i] = rslt[i] - 48;
		}
		String[] result = new String[] { " ", " ", " ", " ", " ", " ", " ", " ", " " };
		int length = result.length - array.length;
		for (int i = 0; i < array.length; i++) {
			result[i + length] = String.valueOf(array[i]);
		}
		result[length - 1] = "￥";
		return result;
	}

	private static final String UNIT = "万千佰拾亿千佰拾万千佰拾元角分";
	private static final String DIGIT = "零壹贰叁肆伍陆柒捌玖";
	private static final double MAX_VALUE = 9999999999999.99D;

	public static String change(String str) {
		double v = Double.valueOf(str);
		if (v < 0 || v > MAX_VALUE) {
			return "参数非法!";
		}
		long l = Math.round(v * 100);
		if (l == 0) {
			return "零元整";
		}
		String strValue = l + "";
		// i用来控制数
		int i = 0;
		// j用来控制单位
		int j = UNIT.length() - strValue.length();
		StringBuffer rs = new StringBuffer("");
		boolean isZero = false;
		for (; i < strValue.length(); i++, j++) {
			char ch = strValue.charAt(i);
			if (ch == '0') {
				isZero = true;
				if (UNIT.charAt(j) == '亿' || UNIT.charAt(j) == '万' || UNIT.charAt(j) == '元') {
					rs.append(UNIT.charAt(j));
					isZero = false;
				}
			} else {
				if (isZero) {
					rs.append("零");
					isZero = false;
				}
				rs.append(DIGIT.charAt(ch - '0')).append(UNIT.charAt(j));
			}
		}
		if (!rs.toString().endsWith("分")) {
			rs.append("整");
		}
		return rs.toString().replaceAll("亿万", "亿");
	}
	
//	public static void main(String[] args) {
////		String[] result1 = getCp("8645");
////    	for (String element : result1) {
////    		System.out.println("==="+element+"===");
////    	}
//    	
//    	String[] result2 = getCp2("200000");
//    	for (String element : result2) {
//    		System.out.println("==="+element+"===");
//    	}
//	}

//	 public static void main(String[] args){
//		 System.out.println(ChineseConvert.change("8123560009.17"));
//	 }
}